var util = require('utils/util.js')
var md5 = require('utils/md5.js')
var spay = require('utils/pay.js')

App({
  onLaunch: function () {
    wx.switchTab({
      url: '/pages/login/login',
    })
    return
    var that = this
    //获取openid，并getuserinfo
    that.getOpenId(that._getUserInfo)
    that.getToken()
    console.log(util.guid())
  },

  onShow: function (options) {
    //判断是否越权
    console.log('App Show', options)
    util.expire(this.exp, options.path, this.missPage)
    wx.redirectTo({
      url: '/pages/index/index',
    })
  },

  onHide: function () {
    console.log('App Hide')
  },

  //获取openid
  getOpenId: function (callback) {
    var that = this
    util.getOpenId('openid', function (res) {
      that.openId = res.data
      typeof callback == 'function' && callback(res.data)
    })
  },

  //获取token
  getToken: function (callback) {
    var that = this
    util.getOpenId('token', function (res) {
      that.token = res.data
      // console.log('token', that.token)
      callback !== undefined ? callback(res.data) : null
    })
  },

  _getUserInfo: function (res) {
    //获取微信用户信息
    util.getUserInfo(this)
  },

  //调起支付接口
  /* options 至少三个参数 
      tenantid //租户ID
      openid   //用户openid
      fee      //支付金额
      body     //购买内容，可以是标题
    */
  testPay: function (options) {
    var that = this
    var timestamp = (new Date().getTime()).toString()
    var rand_str = util.randomStr(32)
    util.getPayId(options.t, options.open_id, options.fee, 'wsxt', function (res) {
      var str = "appId=wxaaf5f79c147f6f2e&nonceStr=" + rand_str + "&package=prepay_id=" + res.data.data.payid + "&signType=MD5&timeStamp=" + timestamp + "&key=60dd010d4454a100b032931e93eef052"
      var paySign = that.md5.hexMD5(str).toUpperCase()
      wx.requestPayment({
        'timeStamp': timestamp,
        'nonceStr': rand_str,
        'package': 'prepay_id=' + res.data.data.payid,
        'signType': 'MD5',
        'paySign': paySign,
        'success': function (res) {
          console.log('succ:', res)
        },
        'fail': function (res) {
          console.log('err:', res)
        }
      })
    })
  },

  //用户信息
  version: '1.3.5',
  openId: null,
  token: null,
  util: util,
  md5: md5,
  spay: spay,
  power: 0,
  userInfo: [],
  login: [],
  exp: 0, //会话过期时间
  school: [],
  grade: [],
  areaChart: null,
  columnChart: null,
  lineChart: null,
  pieChart: null,
  radarChart: null,
  ringChart: null,
  missPage: ['pages/test/Christmas', 'pages/active/details', 'pages/active/payResults', 'pages/login/login', 'pages/home/home'],//token排除页面
});